<template>
  <div class="todoapp">
    <TodoHeader @addFn="addFn" :arr="list"></TodoHeader>
    <TodoMain :arr="showArr" @delFn="delFn"></TodoMain>
    <TodoFooter :farr="showArr" @shai="shai" @clefn="clefn"></TodoFooter>
  </div>
</template>

<script>
//引入
import "./styles/base.css";
import "./styles/index.css";
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("todos")) || [],
      getSel: "all",
    };
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    TodoHeader,
    // eslint-disable-next-line vue/no-unused-components
    TodoMain,
    // eslint-disable-next-line vue/no-unused-components
    TodoFooter,
  },
  methods: {
    //添加功能
    addFn(a) {
      let id =
        this.list.length == 0 ? 100 : this.list[this.list.length - 1] + 1;
      this.list.push({
        id: id,
        name: a,
        idDone: false,
      });
    },
    //删除功能
    delFn(index) {
      this.list.splice(index, 1);
    },
    //筛选功能
    shai(str) {
      this.getSel = str;
    },
    //清空功能
    clefn() {
      this.list = this.list.filter((obj) => obj.isDone == false);
    },
  },
  computed: {
    // eslint-disable-next-line vue/return-in-computed-property
    showArr() {
      if (this.getSel == "yes") {
        //显示已完成
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel == "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        //只要李斯特变化就覆盖式存到本地存储中
        localStorage.setItem("todos", JSON.stringify(this.list));
      },
    },
  },
};
</script>

<style>
</style>